<?php
defined('is_running') or die('Not an entry point...');
include_once("init.php");
include_once("functions.php");

if (isset($_POST['submit'])) {
    if ($_POST['submit'] == "Add")
        addFavourites($_POST['fav_name'],
                $_POST['fav_url'],
                $_POST['fav_cat'],
                $_POST['fav_date']);

    if ($_POST['submit'] == "Del")
        delFavourites($_POST['fav_id']);
}

$cat_lines = readData($cat_data);
?>
<style type="text/css">
    form {
        padding: 0px;
    }
</style>

<table border = "1" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <th>Name</th>
        <th>URL</th>
        <th>Category</th>
        <th>Date</th>
        <th>&nbsp;</th>
    </tr>
    <?php
    $lines = readData($fav_data);

    foreach ($lines as $line) {
        list($fav_id, $fav_name, $fav_url, $fav_cat, $fav_date) = explode("|", $line);
    ?><tr>
            <td><?php echo $fav_name; ?></td>
            <td><a href="<?php echo $fav_url; ?>"><?php echo $fav_url; ?></a></td>
            <td><?php echo $fav_cat; ?></td>
            <td><?php echo $fav_date; ?></td>
            <td><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                    <input type="hidden" name="fav_id" value="<?php echo $fav_id; ?>" />
                    <input type="submit" value="Del" name="submit"/></form>
            </td>
        </tr>
    <?php } ?>

    <tr><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <td><input type="text" name="fav_name" /></td>
        <td><input type="text" name="fav_url" /></td>
        <td><select name="fav_cat">
<?php
    foreach ($cat_lines as $cat_line) {
        list(, $cat_name) = explode("|", $cat_line);
        echo "<option>$cat_name</option>\n";
    }
?>
            </select></td>
        <td><input type="text" name="fav_date" value="<?php echo date("Y-m-d"); ?>" /></td>
        <td><input type="submit" value="Add" name="submit" /></td>
    </form></tr>
</table>
